package com.app.web.user.myCenter;

import com.app.utils.base.j2se.Convert;
import com.app.utils.spring.config.SystemError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tbyy-app->MyCenterService
 * @Auther: 王伟
 * @Date: 2019/12/13 11:00
 * @Description:
 */
@Service
public class MyCenterService {
    @Autowired
    private MyCenterDao myCenterDao;

    //新增收货地址
    public void saveUserAddress(Map<String,Object> map){
        if(Convert.objToInt(map.get("defaultStatus"))==1){
            updateUserAddress(0,Convert.objToInt(map.get("uid")),0);
        }
        if(Convert.objToInt(map.get("id"))>0){
           int count = myCenterDao.updateAddressById(map);
           if(count!=1){
               SystemError.wrapBs("6001","修改收货地址失败");
           }
        }else{
            myCenterDao.saveUserAddress(map);
        }
    }

    //查询收货地址
    public List<Map<String,Object>> selectUserAddress(long uid, int defaultStatus){
        return myCenterDao.selectUserAddress(uid, defaultStatus);
    }

    //删除收货地址
    public int deleteUserAddress(long uid,int id){
        return myCenterDao.deleteUserAddress(uid,id);
    }

    //修改收货地址
    public int updateUserAddress(int id, long uid, int defaultStatus){
        Map<String,Object> param = new HashMap<>();
        if(id>0)param.put("id", id);
        if(uid>0)param.put("uid", uid);
        param.put("defaultStatus", defaultStatus);
        return myCenterDao.updateUserAddress(param);
    }

    public Map<String,Object> addrInfo(long uid, long id){
        return myCenterDao.addrInfo(uid,id);
    }
}
